﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinformMapXpSupportDemo.Model;
namespace WinformMapXpSupportDemo.Forms
{
    public partial class AddDeviceForm : Form
    {
        private MapPosition _devicePosition;

        private Action<DeviceInfo> _AddDeviceCallback;

        private static AddDeviceForm _Instance = null;

        public static AddDeviceForm Create(string deviceName = "", Action<DeviceInfo> addDeviceCallback = null, MapPosition position = null)
        {
            if(_Instance == null || _Instance.IsDisposed)
            {
                _Instance = new AddDeviceForm();
            }
            _Instance.txtDeviceName.Text = deviceName;
            _Instance.lblDeviceCode.Text = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            _Instance._AddDeviceCallback = addDeviceCallback;
            _Instance._devicePosition = position;

            return _Instance;
        }

        private AddDeviceForm()
        {
            InitializeComponent();
        }

        public  void UpdateAddress(string address)
        {
            this.txtDeviceAddress.Text = address;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DeviceInfo deviceInfo = new DeviceInfo()
            {
                DeviceCode = this.lblDeviceCode.Text,
                DeviceName = this.txtDeviceName.Text,
                DevicePosition = this._devicePosition,
                DeviceAddress = this.txtDeviceAddress.Text
            };
            _AddDeviceCallback?.Invoke(deviceInfo);
            this.Hide();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
